package org.fve.ws;

import java.io.IOException;

import org.eclipse.jetty.websocket.WebSocket.Connection;
import org.fve.log.logger;
import org.fve.ws.WebSocketServerHandler.ChatWebSocket;

public class WebsocketTX {

	public void sendTofriendsUsers(String[] messageforfriends,String[] Friendstosend, java.util.Hashtable<Connection, String> tbConnuser,logger log) {				
		for (ChatWebSocket webSocket : WebSocketServerHandler.webSockets) {			    	
			try {				
				String usersend = tbConnuser.get(webSocket.user.getConnection());
				if(usersend!=null){
					for(int i=0;i<Friendstosend.length;i++){
						if(Friendstosend[i].equals(usersend)){									
							webSocket.user.getConnection().sendMessage(messageforfriends[i]);						 
							log.WriteLog("WebsocketTX-->Message send to "+usersend+":"+messageforfriends[i]);
						}
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
			
	    	}
		
	}
	public void sendToUser(Connection connection, String string,logger log) {
		for (ChatWebSocket webSocket : WebSocketServerHandler.webSockets) {				
	    	if(webSocket.user.getConnection().equals(connection)){
	    		try {
					webSocket.user.getConnection().sendMessage(string);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(webSocket.user.getName()!="null"){
	    		 log.WriteLog("WebsocketTX-->Message send to "+webSocket.user.getName()+":"+string);
				}
			}
	    }
	}
	
	public void sendToAllUsers(String string, java.util.Hashtable<Connection, String> tbConnuser,logger log) {
		for (ChatWebSocket webSocket : WebSocketServerHandler.webSockets) {			    	
			try {
				String usersend = tbConnuser.get(webSocket.user.getConnection());
				if(usersend!=null){
				 webSocket.user.getConnection().sendMessage(string);						 
				 log.WriteLog("WebsocketTX-->Message send to "+usersend+":"+string);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
			
	    	}
	    }
	
	
}
